package br.com.mtech.ctrlliberty.ejb.dao;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import br.com.mtech.ctrlliberty.clientjar.entity.AlertaVO;
import br.com.mtech.ctrlliberty.clientjar.entity.TelefoneVO;

/**
 * Session Bean implementation class AlertaEJBDAO
 */
@Stateless
public class AlertaEJBDAO implements AlertaEJBDAOLocal {
	
	@PersistenceContext	
	EntityManager	entityManager;

	@Override
	public AlertaVO inserir(AlertaVO alerta) {
		entityManager.persist(alerta);
		return alerta;
	}
	
	@Override
	public AlertaVO alterar(AlertaVO alerta) {
		return entityManager.merge(alerta);
	}
	
	@Override
	public void excluir(AlertaVO alerta) {
		alerta = entityManager.find(AlertaVO.class, alerta.getId());
		entityManager.remove(alerta);
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<AlertaVO> listarAlertasPorTelefone(TelefoneVO telefone) {
		String strQuery = "from AlertaVO alerta where alerta.telefone = :telefone";
		Query query = entityManager.createQuery(strQuery);
		
		query.setParameter("telefone", telefone);
		
		return query.getResultList();
	}

}
